﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;

namespace DonutLandLibrary.Robots.Environments
{
    public class Square
    {
        private int PowerSourceCount;
        private bool CorrosivePatch;
        private int x;
        private int y;
        private List<Robot> robotCollection;

        public enum renderFormat
        {
            html,
            xml,
            json,
            data
        };

        public int ID { get; set; }

        public List<Robot> RobotsOnSquare
        {
            get
            {
                return robotCollection;
            }
            set
            {
                robotCollection = value;
            }
        }

        public int PowerSources
        {
            get
            {
                return PowerSourceCount;
            }
            set
            {
                PowerSourceCount = value;
            }
        }

        public bool CorrosivePatchPresent
        {
            get
            {
                return CorrosivePatch;
            }
            set
            {
                CorrosivePatch = value;
            }
        }

        public int XCoordinate
        {
            get
            {
                return x;
            }
        }

        public int YCoordinate
        {
            get
            {
                return y;
            }
        }

        public Square(int xcoord, int ycoord)
        {
            x = xcoord;
            y = ycoord;

            robotCollection = new List<Robot>();
        }

        public Square(int powerSourceCount, bool corrosivePatch, int xcoord, int ycoord)
        {
            PowerSourceCount = powerSourceCount;
            CorrosivePatch = corrosivePatch;

            x = xcoord;
            y = ycoord;

            robotCollection = new List<Robot>();
        }

        public void addPowerSources(int powerSourceCount)
        {
            PowerSourceCount += powerSourceCount;
        }

        public void removePowerSource()
        {
            PowerSourceCount--;
        }

        public object renderSquare(Square.renderFormat renderFormat)
        {
            object output = null;

            switch(renderFormat){
                case Square.renderFormat.html:
                    output = renderSquareHTML();

                    break;
                case Square.renderFormat.xml:

                    throw new NotSupportedException("XML output is not currently supported");

                    //break;
                case Square.renderFormat.json:

                    throw new NotSupportedException("JSON output is not currently supported");

                    //break;

                case Square.renderFormat.data:

                    output = renderSquareData();

                    break;
            }

            return output;
        }

        private string renderSquareHTML()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("<td>");

            builder.Append(PowerSourceCount);
            builder.Append("<br />");
            builder.Append(CorrosivePatch.ToString());

            builder.Append("</td>");

            return builder.ToString();
        }

        private string renderSquareData()
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(PowerSourceCount);
            builder.Append(System.Environment.NewLine);
            builder.Append(CorrosivePatch.ToString());

            foreach (Robot robot in robotCollection)
            {
                builder.Append("  ID:");
                builder.Append(robot.ID);
                builder.Append(System.Environment.NewLine);
            }

            //if (RobotOnSquare != null)
            //{
            //    builder.Append("  ID:");
            //    builder.Append(robot.ID);
            //
            //}

            return builder.ToString();
        }
    }

    public class SquareDBContext : DbContext
    {
        public DbSet<Square> Squares { get; set; }
    }
}
